package cn.com.bsoft.cloud.dao.cloud;

import cn.com.bsoft.cloud.UseDatasourceCloud;
import cn.com.bsoft.cloud.entity.cloud.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
@UseDatasourceCloud
public interface CfmessDao {
    /**
     * 更新签名
     */
    @Update("update t_prescription_drug_list set caSign =#{caSign} where prescriptionCode=#{prescriptionCode} or  hisPrescriptionCode=#{prescriptionCode}")
    Boolean updateCaSgin(@Param("prescriptionCode") String prescriptionCode,@Param("caSign") String caSign);

    /**
     * 添加新的处方信息列表
     * @param cf 处方实体类
     * @param yyid //预约ID
     * @return
     */
    @Insert("insert into t_prescription_drug_list(appointId,prescriptionCode,cardNum,hospitalCode,hospitalName,pharmacyCode,pharmacyName,doctorCode,doctorName,patientName,identity,sex,telphone,additionalInfo,notesAndComments,deliveryStatus,paymentStatus,extractingDate,pharmaceuticalCompanyCode,pharmaceuticalCompanyName,\n" +
            "createtime,modifytime,hisPrescriptionCode,checkFlag,caSign,cert,inData)\n" +
            "values(#{yyid},#{cfwyid},#{cf.patient.cardNum},#{cf.patient.hospitalCode},#{cf.patient.hospitalName},#{cf.patient.pharmacyCode},#{cf.patient.pharmacyName},#{cf.doctor.doctorCode},#{cf.doctor.doctorName},#{cf.patient.patientName},#{cf.patient.identity},\n" +
            "#{cf.patient.sex},#{telphone},#{cf.diagnosemess.additionalInfo},#{cf.diagnosemess.notesAndComments},'0','0',#{cf.issuerDate},NULL,NULL,\n" +
            "DATE_ADD(current_timestamp,INTERVAL 8 HOUR),DATE_ADD(current_timestamp,INTERVAL 8 HOUR),#{cf.prescriptionCode},'1',#{cf.caSign},#{cf.cert},#{cf.inData})")
    Boolean addAppointMess(@Param("cf") Cfmesses cf, @Param("yyid") String yyid,@Param("cfwyid") String prescriptionCode,@Param("telphone") String telphone);

    /**
     * 判断预约ID是否存在
     * @param yyid 预约ID
     * @return
     */
    @Select("select count(*) from t_prescription_appointment a,t_prescription_drug_list b where a.appointId = b.appointId and b.deliveryStatus!=3 and a.appointId=#{yyid}")
    Integer yyidIsExists(@Param("yyid") String yyid);

    /**
     * 判断预约ID是否存在
     * @param yyid 预约ID
     * @return
     */
    @Select("select count(*) from t_prescription_appointment where appointId=#{yyid}")
    Integer yyidsfcz(@Param("yyid") String yyid);

    /**
     * 插入预约明细表
     * @param appoint 预约实体类
     * @return
     */
    @Insert("insert into t_prescription_appointment(appointId, appointDate, hospitalCode, hospitalName, departmentCode, departmentName, doctorCode, doctorName, patientName, identity, sex, appointStartTime, appointEndTime, appointStatus, createtime, modifytime,caSign,cert,inData)" +
            "values (#{appoint.appointId},#{appoint.appointDate},#{appoint.hospitalCode},#{appoint.hospitalName},#{appoint.departmentCode},#{appoint.departmentName},#{appoint.doctorCode},#{appoint.doctorName},#{appoint.patientName},#{appoint.identity},#{appoint.sex}," +
            "#{appoint.appointStartTime},#{appoint.appointEndTime},#{appoint.appointStatus},DATE_ADD(current_timestamp,INTERVAL 8 HOUR),DATE_ADD(current_timestamp,INTERVAL 8 HOUR),#{appoint.caSign},#{appoint.cert},#{appoint.inData})")
    Boolean insertAppointInformation(@Param("appoint")Appoint appoint);

    /**
     *
     * @param cfmessDaoList
     * @return
     */
    @Insert("<script>"  +
            "INSERT INTO t_prescription_drug_detail(prescriptionCode,\n" +
            "       drugCode,\n" +
            "       drugName,\n" +
            "       specificationCode,\n" +
            "       specification,\n" +
            "       dispenseAmount,\n" +
            "       dispenseUnitsCode,\n" +
            "       dispenseUnitsName,\n" +
            "       frequencyCode,\n" +
            "       frequencyName,\n" +
            "       aDose,\n" +
            "       doseUnitCode,\n" +
            "       doseUnitName,\n" +
            "       formulationCode,\n" +
            "       formulationName,\n" +
            "       useDescribe,\n" +
            "       medicationDay,\n" +
            "       routeCode,\n" +
            "       routeName,\n" +
            "       unitPrice,\n" +
            "       financialTransaction,\n" +
            "       createtime,\n" +
            "       modifytime) VALUES" +
            "<foreach collection=\"list\" item=\"item1\" index=\"index\"  separator=\",\">" +
            "(#{cfid},#{item1.drugCode},#{item1.drugName},#{item1.specificationCode},#{item1.specification},#{item1.dispenseAmount}," +
            "#{item1.dispenseUnitsCode},#{item1.dispenseUnitsName},#{item1.frequencyCode},#{item1.frequencyName}," +
            "#{item1.aDose},#{item1.doseUnitCode},#{item1.doseUnitName},#{item1.formulationCode},#{item1.formulationName}," +
            "#{item1.useDescribe},#{item1.medicationDay},#{item1.routeCode},#{item1.routeName},#{item1.unitPrice}," +
            "#{item1.financialTransaction},DATE_ADD(current_timestamp,INTERVAL 8 HOUR),DATE_ADD(current_timestamp,INTERVAL 8 HOUR))" +
            "</foreach>" +
            "</script>")
    Boolean cfmxbatchInsert(@Param("list")List<Cfmess> cfmessDaoList,@Param("cfid")String cfid);

    @Update("<script>"+
            "update t_prescription_drug_list\n" +
            "SET paymentStatus=#{param.status},totalFee=#{param.totalFee},selfPay=#{param.selfPay}\n" +
            "WHERE hospitalCode=#{param.hospitalCode} and hisPrescriptionCode in (${codeStr})"+
            "<if test='param.pharmacyCode!=null and param.pharmacyCode.length>0 and param.pharmacyName!=null and param.pharmacyName.length>0 '>"+
            "and pharmacyCode=#{param.pharmacyCode} and pharmacyName=#{param.pharmacyName}"+
            "</if>"+
            "</script>")
    Integer updatePayStaus(@Param("param")UpdatePayStatus updatePayStatus,@Param("codeStr") String codeStr);

    @Select("select hospitalCode,prescriptionCode,paymentStatus status from t_prescription_drug_list where hisPrescriptionCode in (${codeStr}) and hospitalCode=#{param.hospitalCode} and paymentStatus=0")
    List<master> queryCompanyParam(@Param("param")UpdatePayStatus updatePayStatus,@Param("codeStr") String codeStr);

    @Select("select distinct(pharmaceuticalCompanyCode) from t_prescription_drug_list where hisPrescriptionCode in (${codeStr}) and hospitalCode=#{param.hospitalCode} and paymentStatus=0")
    String queryCompanyCode(@Param("param")UpdatePayStatus updatePayStatus,@Param("codeStr") String codeStr);

    @Select("select appId,token,url from t_company_key where companyCode = #{companyCode}")
    Map queryAppIdAndToken(@Param("companyCode")String companyCode);

    @Select("select distinct(deliveryStatus) from t_prescription_drug_list where hisPrescriptionCode in (${codeStr}) and hospitalCode=#{hospitalCode}")
    Integer queryDeliveryStatus(@Param("hospitalCode")String hospitalCode,@Param("codeStr") String codeStr);

    @Update("update t_prescription_drug_list set deliveryStatus=3 where hisPrescriptionCode in (${codeStr}) and hospitalCode=#{hospitalCode}")
    boolean invalidPrescription(@Param("hospitalCode")String hospitalCode,@Param("codeStr") String codeStr);

    /**
     * 判断处方是否存在
     */
    @Select("select count(*) from t_prescription_drug_list where hisPrescriptionCode in (${codeStr}) and hospitalCode=#{hospitalCode}")
    Integer prescriptionIsExists(@Param("hospitalCode")String hospitalCode,@Param("codeStr") String codeStr);

    /**
     * 判断处方是否支付
     */
    @Select("select count(*) from t_prescription_drug_list where hisPrescriptionCode in (${codeStr}) and hospitalCode=#{hospitalCode} and paymentStatus=1")
    Integer prescriptionIsPayed(@Param("hospitalCode")String hospitalCode,@Param("codeStr") String codeStr);

    /**
     * 判断处方是否存在
     */
    @Select("select count(*) from t_prescription_drug_list where hisPrescriptionCode =#{codeStr} and hospitalCode=#{hospitalCode}")
    Integer onePrescriptionIsExists(@Param("hospitalCode")String hospitalCode,@Param("codeStr") String codeStr);
    //判断处方是否作废
    @Select("select count(*) from t_prescription_drug_list where hisPrescriptionCode in (${codeStr}) and hospitalCode=#{hospitalCode} and deliveryStatus=3")
    Integer prescriptionIsDelete(@Param("hospitalCode")String hospitalCode,@Param("codeStr") String codeStr);
    //判断处方是否发药
    @Select("select count(*) from t_prescription_drug_list where hisPrescriptionCode in (${codeStr}) and hospitalCode=#{hospitalCode} and deliveryStatus=2")
    Integer prescriptionIsSend(@Param("hospitalCode")String hospitalCode,@Param("codeStr") String codeStr);
    //判断是否有到达配送点处方
    @Select("select count(*) from t_prescription_drug_list a,t_appoint_site b where a.appointId=b.yyid and a.hisPrescriptionCode=#{hisPrescriptionCode} and a.hospitalCode=#{hospitalCode}")
    Integer prescriptionIsArrived(@Param("hisPrescriptionCode")String hisPrescriptionCode,@Param("hospitalCode")String hospitalCode);
    //查询处方到达配送点信息
    @Results({
            @Result(column="deliveryStatus", property="deliveryStatus"),
            @Result(column="modifytime", property="arrivedTime"),
            @Result(column="psdbm", property="psdbm"),
            @Result(column="hisPrescriptionCode", property="hisPrescriptionCode")
    })
    @Select("select a.deliveryStatus,a.modifytime,b.psdbm,a.hisPrescriptionCode From t_prescription_drug_list a,t_appoint_site b where a.appointId=b.yyid and a.hisPrescriptionCode=#{hisPrescriptionCode} and a.hospitalCode=#{hospitalCode}")
    prescriptionArriveResult prescriptionArrived(@Param("hisPrescriptionCode")String hisPrescriptionCode,@Param("hospitalCode")String hospitalCode);
    //发药到病人手中
    @Update("update t_prescription_drug_list set deliveryStatus=5 where hisPrescriptionCode in (${codeStr}) and hospitalCode=#{hospitalCode}")
    boolean sendDrugs(@Param("hospitalCode")String hospitalCode,@Param("codeStr") String codeStr);
}
